package com.example.yx1.demo38;

import com.example.yx1.demo37.A37;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;

public class A38 {
    /*
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("config", Config.class);
        context.getDefaultListableBeanFactory().setAllowBeanDefinitionOverriding(false);
        context.registerBean(ConfigurationClassPostProcessor.class);
        context.refresh();

        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    //导入第三方配置类
    @Configuration
    @Import(MyImportSelector.class)
    static class Config {

    }

    //负责引入用配置文件配置的外部配置类

    static class MyImportSelector implements DeferredImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            List<String> names = SpringFactoriesLoader.loadFactoryNames(MyImportSelector.class, null);
            return names.toArray(new String[0]);
        }
    }

    static class MyCondition1 implements Condition{

        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            // 测试：存在xx类就返回真
            Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnClass.class.getName());
            String className = attributes.get("className").toString();
            boolean exists = (boolean)attributes.get("exists");
            boolean present = ClassUtils.isPresent(className, null);
            return exists ? present : !present;
        }
    }


    // 自定义注解
    @Target({ElementType.METHOD,ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Conditional(MyCondition1.class)
    @interface ConditionalOnClass {
        boolean exists(); // true 存在 ； false  不存在
        String className(); // 要判断的类型
    }

    //第三方配置类
    @Configuration
    @ConditionalOnClass(className = "org.springframework.boot.context.event.ApplicationContextInitializedEvent",exists = true)
    static class AutoConfig {
        @Bean
        @ConditionalOnMissingBean
        public Bean1 bean1() {
            return new Bean1();
        }
    }

    //第三方配置类
    @Configuration
    @ConditionalOnClass(className = "org.springframework.boot.context.event.ApplicationContextInitializedEvent",exists = false)
    static class AutoConfig1 {
        @Bean
        public Bean2 bean2() {
            return new Bean2();
        }
    }


    static class Bean1 {}
    static class Bean2 {}

     */
}
